const api = require('../../common/api');


const fabric_query = require('../../common/service/fabric_query');
const fabric_util = require('../../common/service/fabric_util');
const myJson = require('../../common/json');


module.exports = {

    list: async function (ctx) {

        let condition = {};
        condition.Table = "company";
        if (ctx.request.query.CompanyName !== undefined && ctx.request.query.CompanyName !== "") {
            condition.CompanyName = ctx.request.query.CompanyName;
        }

        let args = [JSON.stringify(condition)];
        let obj = fabric_util.getObj("department", "CompanyQuery", args)
        let res = await fabric_query.query(obj).then((res) => {
            return res;
        });
        if (myJson.isJSON(res)) {
            ctx.body = {
                "error_code": 0,
                "data": {
                    "list": JSON.parse(res)
                }
            }
        } else {
            ctx.body = {
                "error_code": 20000,
                "error_msg": res
            }
        }

    },
    add: async function (ctx) {
        if (!api.isset(ctx.request.body, ["CompanyName"])) {
            ctx.body = {"error_code": 20000, "error_msg": "parameters is not enough!"};
            return;
        }


        let args = [JSON.stringify({
            "Table": "company",
            "CompanyName": ctx.request.body.CompanyName,
        })];
        let obj = fabric_util.getObj("department", "CompanyAdd", args);

        let res = await fabric_query.invoke(obj).then((res) => {
            return res;
        });
        ctx.body = {
            error_code: 0,
            data: res
        }
    },

    update: async function (ctx) {
        if (!api.isset(ctx.request.body, ["key"])) {
            ctx.body = {"error_code": 20000, "error_msg": "parameters is not enough!"};
            return;
        }
        let condition = {};
        if (ctx.request.body.CompanyName != undefined && ctx.request.body.CompanyName != "") {
            condition.CompanyName = ctx.request.body.CompanyName;
        }
        ;
        let args = [ctx.request.body.key, JSON.stringify(condition)];
        let obj = fabric_util.getObj("department", "CompanyUpdate", args);

        let res = await fabric_query.invoke(obj).then((res) => {
            return res;
        });
        ctx.body = {
            error_code: 0,
            data: res
        }
    }
};